<script setup lang="jsx">
  import {watch,inject,ref,reactive} from 'vue'
  import { Message } from '@arco-design/web-vue'
  import priceDate from "@/api/logistics/priceDate";
  import TimeInfo from '@/views/logistics/price/components/timeinfo.vue'
  import tool from '@/utils/tool'
  import commonApi from '@/api/common'
  import shipment from '@/api/document/shipment'
  import uploadConfig from '@/config/upload'
  const crudRef = ref()
  const timeInfo = ref()
  const record = inject('record')
  watch(record,(newVal,oldVal)=>{
    if(newVal.id != undefined && newVal.id != oldVal.id){
      console.log('刷新运价时间列表--'+record.value.id)
      crudRef.value.requestData()
    }
  })

  const openList = (record) => {
    timeInfo.value.open(record)
  }

  let options = reactive({
      id: 'pricelist_id',
      pk: 'id',
      pageSize:100,
      showTools:false,
      expandSearch:false,
      formOption: {
        viewType: 'drawer',
        width: "70%"
      },
      // 右键菜单配置
      contextMenu: {
        // 是否开启右键菜单
        enabled: false,
      },
      isDbClickEdit:false,
      operationColumn: true,
      operationColumnWidth: 160,
      tablePagination:{style:{display:"none"}},
      api: priceDate.getList,
      beforeRequest:(param)=>{
        param.price_id = record.value.id
        return param;
      },
      beforeAdd:(param)=>{
        param.price_id = record.value.id
        return param;
      },
      add: {
        show: false,
        api: priceDate.save,
        auth: ['logistics:priceDate:save'],
      },
      edit: {
        show: true,
        api: priceDate.update,
        auth: ['logistics:priceDate:update']
      },
      delete: {
        show: true,
        batch: false,
        api: priceDate.deletes,
        auth: ['logistics:priceDate:delete']
      },
    })

const columns = ref([
  {
    title: "服务",
    dataIndex: "service",
    addDisplay:false,
    editDisplay:false,
    disabled: true,
    addDefaultValue:record.value.service,
    formType: "select",
    virtualListProps:{},
    dict:{
        url: 'logistics/service/list',
        props: {label: 'name', value: 'id'},
        params:{
          select:'id,name'
        },
        translation: true,
    },
    hide:true,
  },
  {title: '拣货起始时间',
    dataIndex: "start_time",
    formType: "date",
    showTime: true,
    commonRules: {required: true, message: "请选择拣货起始时间"},
    // hide:true,
  },
  {title: '拣货截止时间',
    dataIndex: "end_time",
    formType: "date",
    showTime: true,
    commonRules: {required: true, message: "请选择拣货截止时间"},
  },
  {title: '下单起始时间',
    dataIndex: "create_start_time",
    formType: "date",
    showTime: true,
    customRender: ({record}) => {
      return record.create_start_time ? record.create_start_time : '-'
    },
    // hide:true,
  },
  {title: '下单截至时间',
    dataIndex: "create_end_time",
    formType: "date",
    showTime: true,
    customRender: ({record}) => {
      return record.create_end_time ? record.create_end_time : '-'
    },
  },
  {
      formType: 'grid-tailwind',
      colNumber: 6,
      cols: [
        {
          formList: [
            {
                title: "文件",
                dataIndex: "file",
                type: 'file',
                formType: "upload",
                // required: true,
                // rules:{required: true,message:'请上传价格文件'}
            },
          ]
        },
        {
          formList: [
            {
              title: '下载模板',
              formType: 'button',
              onClick: (from) => {
                downloadImportTemplate('priceExecl')
              }
              },
          ]
        },
      ],
    },
  {title: '状态',
    dataIndex: "status",
    formType: "select",
    addDefaultValue:1,
    addDisplay:false,
    dict:{
      data:[
        {label: '启用',value:1},
        {label: '关闭',value:2},
      ],
      translation: true,
    },
    showTime: true,
  },
  {
    title: "备注",
    dataIndex: "remark",
    formType: "textarea",
  },
  {title: '创建时间',
    dataIndex: "created_at",
    formType: "date",
    addDisplay:false,
    editDisplay:false,
    showTime: true,
  },
]);

const downloadImportTemplate = async (type) => {
    let res = await shipment.downloadTemplate({type:type})
    let url = res.data.url
    let storeMode = 'OSS'
  
    Message.info('处理中...')
    const domain = uploadConfig.storage[storeMode]
    url = domain + url
    const response = await commonApi.download(url, 'get')
    if (response) {
      tool.download(response, (storeMode !== 'LOCAL') ? res.data.origin_name : '')
      Message.success('请求成功，文件开始下载')  
    } else {
      Message.error('文件下载失败')  
    }
  }
</script>

<template>
  <a-grid-item :span="24">
    <a-collapse :default-active-key="['1']" class="g-collapse-no-p">
      <a-collapse-item header="运价" key="1">
        <template #extra>
          <a-button-group>
            <a-button type="primary" size="small" @click.stop="crudRef.addAction()" v-auth="['logistics:priceDate:save']">添加</a-button>
          </a-button-group>
        </template>
          <ma-crud :options="options" :columns="columns" ref="crudRef" class="file-list">
            <template #operationBeforeExtend="{ record }">
              <a-link @click="openList(record)" >价格查看</a-link>
            </template>
          </ma-crud>
      </a-collapse-item>
    </a-collapse>
  </a-grid-item>
  <time-info ref="timeInfo"></time-info>
</template>
<style>
.file-list ._crud-header,.file-list .mb-2,.file-list .operation-tools,.file-list .mb-1{
  display:none;
}

</style>